import { orderPay, confirming, creatOrder,payReslut } from './goods.js' 
import { router } from '@/router'


export async function payCreatOrder(data) {
	uni.showLoading({
		title:'支付加载中',
		icon:'none'
	})
	const res = await creatOrder(data)
	if(res) {
		console.log(res.data);
	   payApp(res.data.pay,res.data.order.id)
	}
}

export async function payformOrder(order_id) {
	uni.showLoading({
		title:'支付加载中',
		icon:'none'
	})
	const res = await orderPay({
		order_id:order_id
	})
	if(res) {
	   payApp(res.data,order_id)
	}
}
function payApp(obj,order_id) {
	console.log('payApp参数',obj);
	uni.hideLoading()
	wx.requestPayment({
		appId: obj.appId,
		timeStamp: obj.timeStamp,
		nonceStr: obj.nonceStr,
		package: obj.package,
		signType: obj.signType,
		paySign: obj.paySign,
		async success(res) {
			uni.showToast({
				title: '支付成功',
				icon: 'none',
				success() {}
			});
			confirming(order_id).then(() => {
				payReslut(order_id)
			});
			
			router.push('/pages/payResult/payResult?type=1')
		},
		fail(res) {
			console.log('支付失败',res);
			// const { currentRoute } = router;
			// const pathfull = currentRoute.fullPath;
			// if(currentRoute.meta.auth) {
			// 	Cache.set('blackPath',pathfull)
			// 	router.push('/pages/login/login')
			// }
			router.push('/pages/payResult/payResult?type=2')
		}
	});
}

